package com.ai.aichat.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.List;

@Configuration
public class CorsConfig {
    @Bean
    public CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration cfg = new CorsConfiguration();
        cfg.setAllowedOrigins(List.of("http://localhost:5173")); // 允许的前端来源（按需替换）
        cfg.setAllowedMethods(List.of("GET","POST","PUT","DELETE","OPTIONS")); // 允许的方法
        cfg.setAllowedHeaders(List.of("*")); // 允许的请求头（包含 Authorization）
        cfg.setAllowCredentials(true); // 是否允许携带凭据
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", cfg); // 应用于所有路径
        return source;
    }
}